const path = require('path')
const fs = require('fs');
module.exports = class TestNow {
  genJestSource (sourcePath = path.resolve('./')) {
    const testPath = `${sourcePath}/__test__`
    if (!this.fs.existSync(testPath)) {
      fs.mkdirSync(testPath);
    }
    // 遍历代码文件
    let list = fs.readdirSync(sourcePath);
    // 添加完整路径
    list.map(v => `${sourcePath}/${v}`)
    // 过滤文件
    .filter(v => fs.statSync(v).isFile())
    // 排除测试代码
    .filter(v => v.indexOf('.spec') === '-1')
    .map(v => this.genTestFile(v))
  }

  genTestFile (filename) {
    console.log('filename: ', filename);
    const testFileName = this.getTestFileName(filename);
    // 判断此文件是否存在
    if (this.fs.existSync(testFileName)) {
      console.log('该测试代码已存在', testFileName);
      return;
    }
    const mod = require(filename);
    let source;
    if (typeof mod === 'object') {
      source = Object.keys(mod).map()
    } else if (typeof mod === 'function') {

    }
  }
  /**
   * sheng生成测试方法
   * @param {string} methodName 方法名
   * @param {*} classFile 类
   * @param {boolean} isClass 是否是class
   */
  getTestSource (methodName, classFile, isClass = false) {
    console.log('getTestSource: ', methodName);
    return `
      test('${'TEST' + methodName}', () => {
        const ${isClass ? '{'+ methodName +'}' : methodName} = require('${'../' + classFile}');
        const ret = ${methodName}();
        // 断言
        // expect(ret)
        //  .toBe('test return');
      })
    `;
  }
  /**
   * 生成测试文件名
   * @param {string} filename 文件名
   */
  getTestFileName (filename) {
    const dirName = path.dirname(filename)
    const baseName = path.basename(filename)
    const extName = path.extname(filename)
    const testName = baseName.replace(extName, `.spec${extName}`)
    return path.format({
      root: dirName + '/__test__/',
      base: testName
    })
  }
}